package geowiki;

import geowiki.data.Link;
import geowiki.data.LinkExtractor;
import geowiki.data.Place;
import geowiki.data.PlaceExtractor;
import geowiki.data.YQLLinkExtractor;
import geowiki.data.YQLPlaceExtractor;
import geowiki.data.YQLQuery;
import geowiki.utils.WikipediaUtils;
import geowiki.vis.PlaceMarker;

import java.util.ArrayList;
import java.util.List;

public class SimpleGeowikiAppwithLinkedPlaces extends SimpleGeowikiApp {

	/** The util to extract links with. */
	LinkExtractor linkExtractor;

	List<PlaceMarker> linkedPlaceMarkers = new ArrayList<PlaceMarker>();

	public void setup() {
		super.setup();

		worldMap = loadImage("worldmap-equirectangular-blank-dark.png");

		linkExtractor = new YQLLinkExtractor(this);
		PlaceExtractor placeExtractor = new YQLPlaceExtractor(this);

		String wikipediaArticleName = "Oskar_Schlemmer";
		String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
		String restURL = YQLQuery.getAllPlacesFromWebpage(wikipediaURL);

		List<Place> places = placeExtractor.getPlaces(restURL);
		createPlaceMarkers(places, 255, 0, 0);

		// getting the Links and storing into links List
		restURL = YQLQuery.getAllLinksFromWebpage(wikipediaURL);
		List<Link> links = linkExtractor.getLinks(restURL);

		// creating a linkedPlaces Array for each link
		List<List<Place>> linkedPlaces = new ArrayList<List<Place>>();

		// trying to forward the places given in links

		for (int i = 0; i < links.size(); i++) {

			println(links.get(i) + " /" + i + " of " + links.size());
			String linkURL = WikipediaUtils.getFullURLFromLink(links.get(i).url);
                        println("fullURL:"+linkURL);
			restURL = YQLQuery.getAllPlacesFromWebpage(linkURL);

			List<Place> placesOfLink = placeExtractor.getPlaces(restURL);
			println("PlacesFound:" + placesOfLink);
			linkedPlaces.add(i, placesOfLink);

			// createLinkedPlaceMarkers(linkedPlaces.get(i), (int) random(255), (int) random(255), (int) random(255));
			createLinkedPlaceMarkers(linkedPlaces.get(i), 0, (int) random(255), (int) random(255));
		}
		println("placecreated");
		noLoop();
	}

	public void draw() {
		background(0);
		image(worldMap, 0, 0, width, height);
		println("points=" + placeMarkers.size());

		PlaceMarker pm = (PlaceMarker) linkedPlaceMarkers.get(0);
		pm.displaySmall();

		// ** LineDensity according to overall liks, to avoid too heavy drawings
		float lineDensity = map(linkedPlaceMarkers.size(), 0, 5000, 25, 3);

		for (int i = 1; i < linkedPlaceMarkers.size(); i++) {
			PlaceMarker pmone = (PlaceMarker) linkedPlaceMarkers.get(i - 1);
			PlaceMarker pmtwo = (PlaceMarker) linkedPlaceMarkers.get(i);

			stroke(pmone.red, pmone.green, pmone.blue, lineDensity);
			// stroke(0,0,255,lineDensity);
			line(pmone.getX(), pmone.getY(), pmtwo.getX(), pmtwo.getY());
			pmone.displaySmall();
		}

		try {
			pm = (PlaceMarker) placeMarkers.get(0);
			pm.displaySmall();
			for (int i = 1; i < placeMarkers.size(); i++) {
				PlaceMarker pmone = (PlaceMarker) placeMarkers.get(i - 1);
				PlaceMarker pmtwo = (PlaceMarker) placeMarkers.get(i);

				strokeWeight(1);
				stroke(255, 0, 0);
				line(pmone.getX(), pmone.getY(), pmtwo.getX(), pmtwo.getY());
				pmone.displaySmall();
			}

		} catch (Exception e) {
		}

	}

	/**
	 * Creates place markers from the given places.
	 */
	void createPlaceMarkers(List<Place> places, int red, int green, int blue) {
		println("placeCount:" + places.size());
		for (int i = 0; i < places.size(); i++) {
			Place place = places.get(i);
			PlaceMarker placeMarker = new PlaceMarker(this, place, red, green, blue);
			println("place=" + place);
			placeMarkers.add(placeMarker);
		}
	}

	void createLinkedPlaceMarkers(List<Place> places, int red, int green, int blue) {
		println("linkedPlaceCount:" + places.size());
		for (int i = 0; i < places.size(); i++) {
			Place place = places.get(i);
			PlaceMarker placeMarker = new PlaceMarker(this, place, red, green, blue);
			println("place=" + place);
			linkedPlaceMarkers.add(placeMarker);
		}
	}

}
